package com.reservation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.entity.GuideReservation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

/**
 * 解说预约Mapper接口
 */
@Mapper
public interface GuideReservationMapper extends BaseMapper<GuideReservation> {
    
    /**
     * 根据用户ID查询预约列表
     *
     * @param page   分页参数
     * @param userId 用户ID
     * @return 预约列表
     */
    @Select("SELECT gr.* FROM guide_reservation gr WHERE gr.user_id = #{userId} AND gr.deleted = 0 ORDER BY gr.create_time DESC")
    IPage<GuideReservation> selectPageByUserId(Page<GuideReservation> page, @Param("userId") Long userId);
    
    /**
     * 根据解说ID查询预约列表
     *
     * @param page    分页参数
     * @param guideId 解说ID
     * @return 预约列表
     */
    @Select("SELECT gr.* FROM guide_reservation gr WHERE gr.guide_id = #{guideId} AND gr.deleted = 0 ORDER BY gr.create_time DESC")
    IPage<GuideReservation> selectPageByGuideId(Page<GuideReservation> page, @Param("guideId") Long guideId);
    
    /**
     * 根据展览ID查询预约列表
     *
     * @param page        分页参数
     * @param exhibitionId 展览ID
     * @return 预约列表
     */
    @Select("SELECT gr.* FROM guide_reservation gr WHERE gr.exhibition_id = #{exhibitionId} AND gr.deleted = 0 ORDER BY gr.create_time DESC")
    IPage<GuideReservation> selectPageByExhibitionId(Page<GuideReservation> page, @Param("exhibitionId") Long exhibitionId);
    
    /**
     * 根据解说ID和日期查询当日预约次数
     *
     * @param guideId        解说ID
     * @param reservationDate 预约日期
     * @return 预约次数
     */
    @Select("SELECT COUNT(*) FROM guide_reservation WHERE guide_id = #{guideId} AND reservation_date = #{reservationDate} AND status = 'APPROVED' AND deleted = 0")
    Integer countReservationsByGuideIdAndDate(@Param("guideId") Long guideId, @Param("reservationDate") LocalDate reservationDate);
    
    /**
     * 根据解说ID、展览ID和状态查询预约列表
     *
     * @param page        分页参数
     * @param guideId      解说ID
     * @param exhibitionId 展览ID
     * @param status      预约状态
     * @return 预约列表
     */
    @Select("SELECT gr.* FROM guide_reservation gr WHERE gr.guide_id = #{guideId} AND gr.exhibition_id = #{exhibitionId} AND gr.status = #{status} AND gr.deleted = 0 ORDER BY gr.create_time DESC")
    IPage<GuideReservation> selectPageByGuideIdAndExhibitionIdAndStatus(Page<GuideReservation> page, @Param("guideId") Long guideId, @Param("exhibitionId") Long exhibitionId, @Param("status") String status);
}